'''
dionysus - a web based dialogue management framework
Copyright (C) 2011  Eugeniu Costetchi <eugeniu.costetchi (at) tudor.lu>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Created on Dec 31, 2010
'''
from xml.dom.minidom import parse
from DialogueManager.QAF import QafTemplate
from DialogueManager.utils.properties import QAF_RESOURCE_FILE
from DialogueManager.utils.log_utils import log_debug

def get_qaf_array():
    with open(QAF_RESOURCE_FILE) as data_source:
        result=[]
        dom = parse(data_source)
        assert dom.documentElement.tagName == "qaf_templates"
        templates_document=dom.documentElement
        for qaf_tag in templates_document.getElementsByTagName('qaf'):
            try:
                about=qaf_tag.getAttribute('about')
                answer_type=qaf_tag.getAttribute('answer_type')
                question_nlt=qaf_tag.getElementsByTagName('question_nlt')[0].firstChild.nodeValue
                fact_nlt=qaf_tag.getElementsByTagName('fact_nlt')[0].firstChild.nodeValue
                fact_flt=qaf_tag.getElementsByTagName('fact_flt')[0].firstChild.nodeValue
                qaf = QafTemplate(about,answer_type,question_nlt,fact_nlt,fact_flt)
                result.append(qaf) 
            except:
                log_debug('could not read a qaf from file',exception=True)
        return result

def qaf_array_to_dict(qaf_array):
    '''     returns a dict like this 
            'about':[instance1,instance2,...] 
    '''
    res = {} 
    for i in qaf_array:
        if not res.has_key(i.about):
            res[i.about]=[]
        res[i.about].append(i)
    return res

#q = get_qaf_array()
#print q
#w= qaf_array_to_dict(q)
#print w
#e = sample(w['meronimy'], 1)[0]
#print e

#x = get_qaf_array()
#x[0].instantiate({'object':'dog'})
#pass